package edu.uit.se.laps.web.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.uit.se.laps.web.controller.ShoppingCart;
import edu.uit.se.laps.web.controller.TaiKhoanManager;
import edu.uit.se.laps.web.controller.TaiKhoanSession;
import edu.uit.se.laps.web.model.TaiKhoan;

public class TaiKhoanAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3097089778767827260L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private TaiKhoan taiKhoan;
	private List<TaiKhoan> taiKhoanList;
	private int idUser;

	private String message;

	private TaiKhoanManager taiKhoanManager;

	public TaiKhoanAction() {
		taiKhoanManager = new TaiKhoanManager();
	}

	public String excute() {
		taiKhoanList = taiKhoanManager.list();
		System.out.println("excute called");
		return SUCCESS;
	}

	public String getalluser() {
		taiKhoanList = taiKhoanManager.list();
		System.out.println("get all users called");
		return SUCCESS;
	}

	public String showupdate() {
		// Lay id tai khoan tu request
		String idUserStr = request.getParameter("idUser");

		int idUser = Integer.parseInt(idUserStr);
		TaiKhoan tk = taiKhoanManager.getUser(idUser);

		HttpSession session = request.getSession();
		TaiKhoanSession tkSession = (TaiKhoanSession) session
				.getAttribute("TaiKhoanSession");
		tkSession.setTkTemp(tk);
		session.setAttribute("TaiKhoanSession", tkSession);

		return SUCCESS;
	}

	public String addUser() {

		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String hoTen = request.getParameter("hoTen");
		String gioiTinh = request.getParameter("gioiTinh");
		String dienThoai = request.getParameter("dienThoai");
		String diaChi = request.getParameter("diaChi");
		String ngaySinh = request.getParameter("ngaySinh");
		
		
		boolean isGioiTinh;
		if (gioiTinh.equals("1"))	//Gioi tinh: Nu
			isGioiTinh = true;
		else
			isGioiTinh = false;
		
		TaiKhoan tk = new TaiKhoan();
		tk.setEmail(email);
		tk.setPassword(password);
		tk.setHoTen(hoTen);
		tk.setGioiTinh(isGioiTinh);
		
		tk.setDienThoai(dienThoai);
		tk.setDiaChi(diaChi);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date ngaysinh = format.parse(ngaySinh);
			System.out.println("" + ngaysinh);
			tk.setNgaySinh(ngaysinh);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// System.out.println(getTaiKhoan());
		// String email = taiKhoan.getEmail();
		try {
			TaiKhoan tk_temp = (TaiKhoan) taiKhoanManager.getUser(email);
			// tai khoan da co nguoi su dung
			if (tk_temp != null) {
				message = "Tài khoản đã có người dùng";
				this.taiKhoanList = taiKhoanManager.list();
				return ERROR;
			} else {
				taiKhoanManager.add(tk);
				
				HttpSession session = request.getSession();
				// get the taikhoan session
				TaiKhoanSession tkSession = (TaiKhoanSession) session
						.getAttribute("TaiKhoanSession");
				tkSession.setEmail(tk.getEmail());
				session.setAttribute("TaiKhoanSession", tkSession);
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		this.taiKhoanList = taiKhoanManager.list();
		System.out.println("add user called");
		return SUCCESS;
	}

	public String add() {

		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String hoTen = request.getParameter("hoTen");
		String gioiTinh = request.getParameter("gioiTinh");
		String dienThoai = request.getParameter("dienThoai");
		String diaChi = request.getParameter("diaChi");
		String ngaySinh = request.getParameter("ngaySinh");
		
		boolean isGioiTinh;
		if (gioiTinh.equals("1"))	//Gioi tinh: Nu
			isGioiTinh = true;
		else
			isGioiTinh = false;
		
		TaiKhoan tk = new TaiKhoan();
		tk.setEmail(email);
		tk.setPassword(password);
		tk.setHoTen(hoTen);
		//tk.setGioiTinh(gioiTinh);
		tk.setGioiTinh(isGioiTinh);
		tk.setDienThoai(dienThoai);
		tk.setDiaChi(diaChi);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date ngaysinh = format.parse(ngaySinh);
			System.out.println("" + ngaysinh);
			tk.setNgaySinh(ngaysinh);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// System.out.println(getTaiKhoan());
		// String email = taiKhoan.getEmail();
		try {
			TaiKhoan tk_temp = (TaiKhoan) taiKhoanManager.getUser(email);
			// tai khoan da co nguoi su dung
			if (tk_temp != null) {
				message = "Tài khoản đã có người dùng";
				this.taiKhoanList = taiKhoanManager.list();
				return ERROR;
			} else {
				taiKhoanManager.add(tk);

			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		this.taiKhoanList = taiKhoanManager.list();
		System.out.println("add user called");
		return SUCCESS;
	}

	public String delete() {
		// get id user tu request
		String idUserStr = request.getParameter("idUser");
		int id = Integer.parseInt(idUserStr);

		taiKhoanManager.delete(id);
		System.out.println("delete user called");
		return SUCCESS;
	}

	// ---------//
	public String update() {
		// get id user tu request
		String idUserStr = request.getParameter("idUser");
		int idUser = Integer.parseInt(idUserStr);

		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String hoTen = request.getParameter("hoTen");
		String gioiTinh = request.getParameter("gioiTinh");
		String dienThoai = request.getParameter("dienThoai");
		String diaChi = request.getParameter("diaChi");
		String ngaySinh = request.getParameter("ngaySinh");
		
		
		boolean isGioiTinh;
		if (gioiTinh.equals("1"))	//Gioi tinh: Nu
			isGioiTinh = true;
		else
			isGioiTinh = false;
		
		TaiKhoan tk = new TaiKhoan();
		tk.setIdUser(idUser);
		tk.setEmail(email);
		tk.setPassword(password);
		tk.setHoTen(hoTen);
		tk.setGioiTinh(isGioiTinh);
		
		tk.setDienThoai(dienThoai);
		tk.setDiaChi(diaChi);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date ngaysinh = format.parse(ngaySinh);
			System.out.println("" + ngaysinh);
			tk.setNgaySinh(ngaysinh);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// System.out.println(getTaiKhoan());
		try {
			// TaiKhoan tk_temp = (TaiKhoan) taiKhoanManager.getUser(email);
			// //tai khoan da co nguoi su dung
			// if (tk_temp != null){
			// message = "Tài khoản đã có người dùng";
			// this.taiKhoanList = taiKhoanManager.list();
			// return ERROR;
			// }
			// else {
			// taiKhoanManager.update(tk);
			// }
			taiKhoanManager.update(tk);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.taiKhoanList = taiKhoanManager.list();
		System.out.println("update users called");
		return SUCCESS;
	}

	// ---------//

	public String loginUser() {
		// lay user tu csdl bang email
		String email = taiKhoan.getEmail();
		if (email.equals("admin")){
			message = "Email hoặc password không hợp lệ";
			return ERROR;
		}
		String password = taiKhoan.getPassword();
		TaiKhoan tk = taiKhoanManager.getUser(email, password);
		if (tk != null) {
			HttpSession session = request.getSession();
			// get the taikhoan session
			TaiKhoanSession tkSession = (TaiKhoanSession) session
					.getAttribute("TaiKhoanSession");
			tkSession.setEmail(taiKhoan.getEmail());
			session.setAttribute("TaiKhoanSession", tkSession);

			return SUCCESS;
		} else {
			message = "Email hoặc password không hợp lệ";
		}
		// if (taiKhoan.getEmail().equalsIgnoreCase("admin") &&
		// taiKhoan.getPassword().equalsIgnoreCase("12345")){
		// return SUCCESS;
		// }
		return ERROR;
	}

	public String loginAdmin() {
		if (taiKhoan.getEmail().equalsIgnoreCase("admin")
				&& taiKhoan.getPassword().equalsIgnoreCase("12345")) {

			HttpSession session = request.getSession();

			// get the taikhoan session
			TaiKhoanSession tkSession = (TaiKhoanSession) session
					.getAttribute("TaiKhoanSession");
			tkSession.setEmail(taiKhoan.getEmail());

			session.setAttribute("TaiKhoanSession", tkSession);

			return SUCCESS;
		}
		message = "Email hoặc password không hợp lệ";
		return ERROR;
	}

	public String showLoginAdmin(){
		HttpSession session = request.getSession();
		// get the taikhoan session
		TaiKhoanSession tkSession = (TaiKhoanSession) session
				.getAttribute("TaiKhoanSession");
		tkSession.setEmail("");
		session.setAttribute("TaiKhoanSession", tkSession);

		return SUCCESS;
	}
	
	
	
	
	public TaiKhoan getTaiKhoan() {
		return taiKhoan;
	}

	public void setTaiKhoan(TaiKhoan taiKhoan) {
		this.taiKhoan = taiKhoan;
	}

	public List<TaiKhoan> getTaiKhoanList() {
		return taiKhoanList;
	}

	public void setTaiKhoanList(List<TaiKhoan> taiKhoanList) {
		this.taiKhoanList = taiKhoanList;
	}

	public int getIdUser() {
		return idUser;
	}

	public void setIdUser(int idUser) {
		this.idUser = idUser;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

}
